﻿using UnityEngine;
using System.Collections;

namespace PERSONAJES
{
	
	public class PersonajeJugador : MonoBehaviour {

		//Propiedades
		public static bool moverse;
		public static float posicionX;
		private float speed = 1.5F;
		private int direccion = 1;
		Material RetratoDialogo;
		Animator _animator;

		void Start () {
			_animator = GetComponent<Animator> ();
		}

		void Update() {
			if(moverse == true)
			{
				Desplazarse(posicionX);
				_animator.SetBool("moverse", moverse);
			}
				
			if(posicionX == this.transform.position.x)
			{
				moverse = false;
				_animator.SetBool("moverse", moverse);
			}
		}

		//Métodos

		/// <summary>
		/// Desplazarse the specified pCoordenadaX.
		/// </summary>
		/// <param name="pCoordenadaX">P coordenada x.</param>
		public void Desplazarse(float pCoordenadaX)
		{

			if(pCoordenadaX < transform.position.x && direccion == 1)
			{
				Vector3 theScale = transform.localScale;
				theScale.x *= -1;
				transform.localScale = theScale;
				direccion = -1;
			}

			if(pCoordenadaX > transform.position.x && direccion == -1)
			{
				Vector3 theScale = transform.localScale;
				theScale.x *= -1;
				transform.localScale = theScale;
				direccion = 1;
			}
				
			transform.position = Vector3.MoveTowards(transform.position,new Vector3(pCoordenadaX, transform.position.y, transform.position.z), speed * Time.deltaTime);

		}
	}
}
